package com.key.web_admin.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.common.base.RespResult;
import com.key.common.utils.OddNumbersUtils;
import com.key.modules.dto.AddCustomerStDTO;
import com.key.modules.dto.CarrierPayDTO;
import com.key.modules.dto.CustomerReDTO;
import com.key.modules.entity.CustomerSt;
import com.key.modules.service.CustomerReService;
import com.key.modules.vo.CarrierPayVO;
import com.key.modules.vo.CustomerReVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * tc
 */
@RestController
@RequestMapping("/customerReceivable")
public class CustomerReController {

    @Autowired
    CustomerReService customerReService;

    /**
     * 信息查询
     * @return
     */
    @PostMapping("/findCustomerReceivable")
    public RespResult findCustomerReceivable(@RequestBody CustomerReDTO customerReDTO){
        Map<String, Object> resultMap= customerReService.findCustomerReceivable(customerReDTO);
        RespResult respResult =RespResult.success((Integer)resultMap.get("total"),resultMap.get("data"));
        return respResult;
    }

    /**
     * 创建对账单
     * @return
     */
    @PostMapping("/createCustomerStatement")
    public RespResult createCustomerStatement(@RequestBody AddCustomerStDTO addCustomerStDTO){

        CustomerSt customerSt=new CustomerSt();
        customerSt.setStatementNumber(addCustomerStDTO.getStatementNumber());
        customerSt.setCustomerName("王虎");
        customerSt.setAmountReceivable(addCustomerStDTO.getAmountReceivable());
        customerSt.setCreationTime(OddNumbersUtils.getDay());
        customerSt.setBillingStatus(0);
        customerSt.setInvoiceStatus(0);
        customerSt.setAbnormalState(0);
        customerSt.setPaybackDate( "2011-11-12");

        int i=customerReService.createCustomerStatement(customerSt);
        return i==0?RespResult.error("创建失败！"):RespResult.success();
    }

    /**
     * 导出excel
     * @return
     */
    @GetMapping("/exportData")

    public RespResult exportData(){
        //excel文件名
        String fileName = "D:\\\\客户应收"+System.currentTimeMillis()+".xls";
        CustomerReDTO customerReDTO=new CustomerReDTO();
        customerReDTO.setCurrentPage(0);
        customerReDTO.setPageSize(1000);
        RespResult respResult=findCustomerReceivable(customerReDTO);
        EasyExcel.write(fileName, CustomerReVO.class).sheet("客户应收").doWrite((ArrayList)respResult.getData());
        return RespResult.success("保存成功路径："+fileName);
    }


}
